package hyl.core.safe;

import java.io.InputStream;
import java.io.OutputStream;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
/**
 * 这个是mydes 和mycert的公共函数类
 * 
 * 主要用于加密解密部分
 * @author 37798955@qq.com
 *
 */
public abstract class MySecret {
	protected Cipher _cipher;
	protected String _algorithm;
	public final static int I加密=Cipher.ENCRYPT_MODE;
	public final static int I解密=Cipher.DECRYPT_MODE;
	public String get算法() {
		return _algorithm;
	}

	


	/**
	 * 适合data长度小于256的 明文或者密文
	 * 
	 * @param data
	 * @return
	 * @throws Exception
	 */
	public byte[] do2运算(byte[] data) {
		try {
			return _cipher.doFinal(data);
		} catch (IllegalBlockSizeException |BadPaddingException e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	/**
	 * 适用于长明文或者密文 <br>
	 * 分段加密
	 * 
	 * @param 输入流
	 * @param len
	 * @param done
	 * @return
	 * @throws Exception
	 */
	public void do2运算(InputStream 输入流,OutputStream 输出流, int len) throws Exception {
		// 取得私钥
		final int size = 128;
		byte[] temp = new byte[size];
		int i = len;		
		int k = size;
		while (i > 0) {
			if (i < size) {
				k = i;
				temp = new byte[k];
				输入流.read(temp);
				i -= k;
				输出流.write(_cipher.doFinal(temp));
				break;
			}
			输入流.read(temp);
			i -= k;
			输出流.write(_cipher.doFinal(temp));
		}
	
		输出流.flush();
		输入流.close();	
		输出流.close();

	}

}
